﻿namespace A14_Ex01_4
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    public class BinaryToDecimal
    {
        private static string m_InputNumberStr = string.Empty;
        private static int m_ValidLength = 5;
        private static bool v_IsBinary = false;
        private static bool v_IsLengthValid = false;

        public static void InputNumberStrValidator(string i_StrToValidate)
        {
            if (i_StrToValidate != null)
            {
                PrintIsInputStrBinary(i_StrToValidate);

                PrintIsInputStrLengthValid(i_StrToValidate);

                if (v_IsBinary && v_IsLengthValid)
                {
                    int convertBinaryToDecimalNumber;
                    bool convertBinaryToDecimalNumberResult = false;

                    convertBinaryToDecimalNumberResult = ConvertBinaryToDecimalNumber(i_StrToValidate, out convertBinaryToDecimalNumber);

                    if (convertBinaryToDecimalNumberResult == true)
                    {
                        System.Console.WriteLine(@"The input number in decimal is: {0}{1}", convertBinaryToDecimalNumber, System.Environment.NewLine);
                    }

                    PrintIsNumIsPrime(convertBinaryToDecimalNumber);
                }
            }
        }

        private static void PrintIsNumIsPrime(int i_DecimalNumber)
        {
            bool result = isInputNumberPrime(i_DecimalNumber);
           
            if (result)
            {
                System.Console.WriteLine(@"The input number is a prime number" + System.Environment.NewLine);
            }
            else
            {
                System.Console.WriteLine(@"The input number is not a prime number" + System.Environment.NewLine);
            }
        }


        private static bool isInputNumberPrime(int i_DecimalNumber)
        {
            bool result = false;
            int evenRemainder, currentDivideRemainder;
            Math.DivRem(i_DecimalNumber, 2, out evenRemainder);

            if (!evenRemainder.Equals(0))
            {
                for (int currentDividerNumber = 2; currentDividerNumber <= i_DecimalNumber && result == false; currentDividerNumber++)
                {
                    Math.DivRem(i_DecimalNumber, currentDividerNumber, out currentDivideRemainder);

                    if (!currentDivideRemainder.Equals(0))
                    {
                        result = true;
                    }
                }
            }
            else
            {
                if (i_DecimalNumber.Equals(2))
                {
                    result = true;
                }
            }

            return result;
        }

        private static bool ConvertBinaryToDecimalNumber(string i_InputStr, out int o_ConvertedNum)
        {
            bool result = true;
            o_ConvertedNum = 0;

            try
            {
                o_ConvertedNum = Convert.ToInt32(i_InputStr, 2);
            }
            catch (Exception)
            {
                result = false;
            }

            return result;
        }

        private static void PrintIsInputStrLengthValid(string i_InputStr)
        {
            if (i_InputStr.Length.Equals(m_ValidLength))
            {
                System.Console.WriteLine(@"The input number length is: {0}{1}", m_ValidLength, System.Environment.NewLine);
                v_IsLengthValid = true;
            }
            else
            {
                System.Console.WriteLine(@"The input number length is NOT {0}{1}", m_ValidLength, System.Environment.NewLine);
            }
        }

        private static void PrintIsInputStrBinary(string i_InputStr)
        {
            try
            {
                Convert.ToInt32(i_InputStr, 2);
                System.Console.WriteLine(@"The input number is given in binary base!" + System.Environment.NewLine);
                v_IsBinary = true;
            }
            catch (Exception)
            {
                System.Console.WriteLine(@"The input number is NOT given in binary base!" + System.Environment.NewLine);
            }
        }
    }
}
